package com.yulongtian.interviewWorks.day20220824;


import java.util.ArrayList;
import java.util.List;

/**
 * https://leetcode.cn/problems/remove-duplicate-node-lcci/
 *
 * @author YulongTian
 * @date 2022/8/24 - 15:00
 */
public class InterviewsWork05 {

    public static void main(String[] args) {
        ListNode head1 = new ListNode(1);
        ListNode head2 = new ListNode(1);
        head1.next = head2;
        ListNode head3 = new ListNode(1);
        head2.next = head3;
        ListNode head4 = new ListNode(1);
        head3.next = head4;
        ListNode head5 = new ListNode(2);
        head4.next = head5;
        printList(head1);

        ListNode listNode = removeDuplicateNodes(head1);
        printList(listNode);
    }

    public static void printList(ListNode head) {
        while (head != null) {
            System.out.print(head.val + "-");
            head = head.next;
        }
        System.out.println();
    }

    public static ListNode removeDuplicateNodes(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        List<Integer> list=new ArrayList<>();
        ListNode point=head;
        ListNode pre=head;
        while (point!=null){
            if(!list.contains(point.val)){
                list.add(point.val);
                pre=point;
                point=point.next;
                continue;
            }
            if(list.contains(point.val)){
                point=point.next;
                pre.next=point;
            }
        }
        return head;


    }

}

class ListNode {
    int val;
    ListNode next;

    ListNode(int x) {
        val = x;
    }
}
